/**
 * Copyright (C) 2013 DaiKit.com - daikit4gxt module (admin@daikit.com)
 *
 *         Project home : http://code.daikit.com/daikit4gxt
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *         http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.daikit.daikit4gxt.client.ui.fields;

import java.io.Serializable;

import com.daikit.daikit4gxt.client.DkMain;
import com.daikit.daikit4gxt.client.action.BaseAsyncCallback;
import com.daikit.daikit4gxt.client.action.BasePagingLoader;
import com.daikit.daikit4gxt.client.action.BasePagingToolbar;
import com.daikit.daikit4gxt.client.action.MyPagingRpcCallerWithInput;
import com.daikit.daikit4gxt.client.action.QueryPagingLoadConfig;
import com.daikit.daikit4gxt.client.model.labelprovider.MyAsSimpleStringLabelProvider;
import com.sencha.gxt.data.shared.LabelProvider;
import com.sencha.gxt.data.shared.ListStore;
import com.sencha.gxt.data.shared.loader.PagingLoadConfig;
import com.sencha.gxt.data.shared.loader.PagingLoadResult;
import com.sencha.gxt.widget.core.client.toolbar.PagingToolBar;


/**
 * A cell for {@link MyDynamicAsyncPagingCombobox}
 * 
 * @author tcaselli
 * @version $Revision$ Last modifier: $Author$ Last commit: $Date$
 * @param <B>
 */
public class MyDynamicAsyncPagingComboboxCell<B extends Serializable> extends MyDynamicComboboxCell<B>
{

	private BasePagingToolbar<B, PagingLoadConfig, PagingLoadResult<B>> pagingToolbar;
	private final BasePagingLoader<B, PagingLoadConfig, PagingLoadResult<B>> basePagingLoader;

	/**
	 * Wrapper to {@link #MyDynamicAsyncPagingComboboxCell(ListStore, LabelProvider, MyPagingRpcCallerWithInput)} with a
	 * {@link MyAsSimpleStringLabelProvider}
	 * 
	 * @param store
	 * @param rpcCaller
	 */
	public MyDynamicAsyncPagingComboboxCell(final ListStore<B> store, final MyPagingRpcCallerWithInput<B> rpcCaller)
	{
		this(store, new MyAsSimpleStringLabelProvider<B>(), rpcCaller);
	}

	/**
	 * Constructor
	 * 
	 * @param store
	 * @param labelProvider
	 * @param rpcCaller
	 */
	public MyDynamicAsyncPagingComboboxCell(final ListStore<B> store, final LabelProvider<B> labelProvider,
			final MyPagingRpcCallerWithInput<B> rpcCaller)
	{
		super(store, labelProvider);
		basePagingLoader = initializePagingLoader(getStore(), rpcCaller);
	}

	/**
	 * Constructor
	 * 
	 * @param store
	 * @param labelProvider
	 * @param rpcCaller
	 * @param choicesTemplate
	 * @param cssBundle
	 */
	public MyDynamicAsyncPagingComboboxCell(final ListStore<B> store, final LabelProvider<B> labelProvider,
			final MyPagingRpcCallerWithInput<B> rpcCaller, final MyDynamicComboboxXTemplate<B> choicesTemplate,
			final MyDynamicComboboxCssBundle<?> cssBundle)
	{
		super(store, labelProvider, choicesTemplate, cssBundle);
		basePagingLoader = initializePagingLoader(getStore(), rpcCaller);
	}

	/**
	 * Paging Loader initialization method
	 * 
	 * @param store
	 * @param rpcCaller
	 * @return the {@link BasePagingLoader}
	 */
	protected BasePagingLoader<B, PagingLoadConfig, PagingLoadResult<B>> initializePagingLoader(final ListStore<B> store,
			final MyPagingRpcCallerWithInput<B> rpcCaller)
	{
		final BasePagingLoader<B, PagingLoadConfig, PagingLoadResult<B>> basePagingLoader = new BasePagingLoader<B, PagingLoadConfig, PagingLoadResult<B>>(
				store, "Loading...", DkMain.SCREEN_PAGING_SIZE, false)
		{
			@Override
			protected void doLoad(final PagingLoadConfig pagingLoadConfig, final BaseAsyncCallback<PagingLoadResult<B>> callback)
			{
				rpcCaller.callLoadRpc(((QueryPagingLoadConfig) pagingLoadConfig).getQuery(), pagingLoadConfig, callback);
			}
		};
		setLoader(basePagingLoader);
		setMinChars(0);
		setQueryDelay(DkMain.config().getAsyncComboboxQueryDelayMilliseconds());
		this.pagingToolbar = basePagingLoader.getPagingToolbar();
		setPageSize(basePagingLoader.getPagingToolbar().getPageSize());
		return basePagingLoader;
	}

	/**
	 * To use our own paging toolBar
	 */
	@Override
	protected final PagingToolBar createPagingToolBar(final int pageSize)
	{
		return pagingToolbar == null ? super.createPagingToolBar(pageSize) : pagingToolbar;
	}

	/**
	 * @return the pagingToolbar
	 */
	public BasePagingToolbar<B, PagingLoadConfig, PagingLoadResult<B>> getPagingToolbar()
	{
		return pagingToolbar;
	}

	/**
	 * @return the basePagingLoader
	 */
	public BasePagingLoader<B, PagingLoadConfig, PagingLoadResult<B>> getBasePagingLoader()
	{
		return basePagingLoader;
	}
}
